/**
 * @Title: warningScreenService_ctrl.js
 * @Description: 预警service
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author ex-xieyuwen001
 * @date 2014年11月10日 下午16:59:25
 * @version V1.0
 */

;(function(){
	'use strict';
	
	var app = angular.module('fwa.services');
	app.service('WarningScreenService',['$http','BaseURL','WarningAddService', function($http, baseUrl,WarningAddService){
		var debug = false,
		serv = this,
		debugUrls = {
			getWarningOrg:baseUrl + 'mock/hcpms/warning/warningOrg.json',
			getWarningMedical:baseUrl + 'mock/hcpms/warning/warningMedical.json',
			getWarningBlack:baseUrl + 'mock/hcpms/warning/warningBlack.json'
		},
		urls = {
			getWarningOrg:baseUrl + 'warningOrgBlack/warningOrg.do',
			getWarningMedical:baseUrl + 'warningOrgBlack/warningMedical.do',
			getWarningBlack:baseUrl + 'warningOrgBlack/warningBlack.do',
			getWarnResult:baseUrl + 'warnResult/warnResult.do',
			getWarnMonitor:baseUrl + 'warnResult/warnMonitor.do',
			getWarnMonitorRule:baseUrl + 'warnResult/warnMonitorRule.do',
			getWarnResultDetail:baseUrl + 'warnResult/warnResultDetail.do',
			getWarnExamineResult:baseUrl+'warnExamineResult/warnExamineResult.do',
			getWarnApprovalResult:baseUrl+'warnApprovalResult/warnApprovalResult.do',
			getWarningEditCheckResult:baseUrl+'hcpmsWarning/getFeedBackList.do',
			saveApprovalResult:baseUrl+'warnApprovalResult/saveApprovalResult.do',
			getWarningDeduction:baseUrl+'warningDeduction/warningDeduction.do',//评分标准
			getFindPointCuts:baseUrl+'warningDeduction/findPointCuts.do',//扣分查询
			getFindPointCutTemps:baseUrl+'warningDeduction/findPointCutTemps.do',//临时扣分查询(有限制条件)
			getPointCutTempScose:baseUrl+'warningDeduction/findPointCutTempScose.do',//临时扣分查询
			getPointScose:baseUrl+'warningDeduction/mediCalContentPoint.do',//医生年度周期内已记分值
			getFindIdStandardScores:baseUrl+'warningDeduction/findIdStandardScores.do',//机构对象当前细则对应的考核内容项下所有细则信息
			getFindPointCutStorageTemps:baseUrl+'warningDeduction/findPointCutStorageTemps.do',//暂存记分信息查询
			findPointCutStorageCollections:baseUrl+'warningDeduction/findPointCutStorageCollections.do'//记分临时信息查询(包含暂存记分数据)
		};
		serv.warningOrgs = [];
		serv.warningMedicals = [];
		serv.warningBlacks = [];
		serv.warnMonitors = [];
		serv.warnMonitorRules = [];
		
		//扣分集合
		serv.tableDataDeductions = [];
		
//		  warningDeductionMedical = [],//医疗机构
//	        warningDeductionStaff = [],//医务人员
//	        warningDeductionInsurance = [],//参保人员
		
//		serv.checkedArray = [];
		function getUrl(name) {
			if(debug) {
				return debugUrls[name];
			} else {
				return urls[name];
			}
		};
		serv.getWarningOrg = function(params) {
			return $http({
				url:getUrl('getWarningOrg'),
				method:'GET',
				params : params
			});
		}
		serv.getWarningMedical = function(params) {
			return $http({
				url:getUrl('getWarningMedical'),
				method:'GET',
				params : params
			});
		}
		serv.getWarningBlack = function(params) {
			return $http({
				url:getUrl('getWarningBlack'),
				method:'GET',
				params : params
			});
		}
		serv.getWarnResult = function(params) {
			return $http.post(getUrl('getWarnResult'),params);
		}
		serv.getWarnMonitor = function() {
			$http({
		    	 method: "get",
		    	 url:getUrl('getWarnMonitor')
			}).success(function(response, status, headers, config){
				serv.warnMonitors = response;
			});
		}
		serv.getWarnMonitorRule = function() {
			$http({
		    	 method: "get",
		    	 url:getUrl('getWarnMonitorRule')
			}).success(function(response, status, headers, config){
				serv.warnMonitorRules = response;
			});
		}
		
		serv.getWarnResultDetail = function(params) {
			return $http({
				url:getUrl('getWarnResultDetail'),
				method:'GET',
				params : params
			});
		}
		
		serv.getWarnExamineResult = function(params) {
			return $http({
				url:getUrl('getWarnExamineResult'),
				method:'GET',
				params : params
			});
		}
		serv.getWarnApprovalResult = function(params) {
			return $http({
				url:getUrl('getWarnApprovalResult'),
				method:'GET',
				params : params
			});
		}
		
		serv.saveApprovalResult = function(params) {
//			return $http({
//				url:getUrl('saveApprovalResult'),
//				method:'GET',
//				params : params
//			});
			 return $http.post(getUrl('saveApprovalResult'),params);
		}
		serv.getWarningEditCheckResult = function(params) {
			return $http({
				url:getUrl('getWarningEditCheckResult'),
				method:'GET',
				params : params
			});
		}
		serv.getWarningDeduction = function(params) {
			return $http({
				url:getUrl('getWarningDeduction'),
				method:'GET',
				params : params
			});
		}
		serv.getFindPointCuts = function(params) {
			return $http({
				url:getUrl('getFindPointCuts'),
				method:'GET',
				params : params
			});
		}
		serv.getFindPointCutTemps = function(params) {
			return $http({
				url:getUrl('getFindPointCutTemps'),
				method:'GET',
				params : params
			});
		}
		serv.getPointCutTempScose = function(params) {
			return $http({
				url:getUrl('getPointCutTempScose'),
				method:'GET',
				params : params
			});
		}
		serv.getPointScose = function(params) {
			return $http({
				url:getUrl('getPointScose'),
				method:'GET',
				params : params
			});
		}
		serv.getFindIdStandardScores = function(params) {
			return $http({
				url:getUrl('getFindIdStandardScores'),
				method:'GET',
				params : params
			});
		}
		serv.getFindPointCutStorageTemps = function(params) {
			return $http({
				url:getUrl('getFindPointCutStorageTemps'),
				method:'GET',
				params : params
			});
		}
		serv.findPointCutStorageCollections = function(params) {
			return $http({
				url:getUrl('findPointCutStorageCollections'),
				method:'GET',
				params : params
			});
		}
		//展示扣分信息
		//params:warningMarks:WarningAddService.warningMarks,caseNo:案件编号,caseType:案件类型
		//warnMedicalData:机构信息,warnStaffData:医护人员,warnInsurData:参保人
		serv.getIdPointCuts = function(data,num){//num:0.删除，1.新增
			if(num == 0){
				return {idPointCut:data.idPointCut,remainingScores:data.remainingScores,caseType:data.caseType,caseNo:data.caseNo};
			}else if(num == 1){
				return {idPointCut:data.idPointCut,remainingScores:data.point,caseType:data.caseType,caseNo:data.caseNo};
			}
		}
		serv.initIsTrue = function(data,caseType,datas,caseNo){
			if(caseType == '01'){
				return (data.code == datas.targetNo && caseNo == datas.caseNo && caseType == datas.caseType);
			}else if(caseType == '02'){
				return (data.selfCode == datas.targetNo && caseNo == datas.caseNo && caseType == datas.caseType);
			}
			return false;
		}
		
		//操作及暂存数据收集--删除
		serv.intiDeductionDeletes = function(pointCutTemps,deductionDeletes){
			if(pointCutTemps){
				angular.forEach(pointCutTemps,function(data,index){
					 if(data.delIsTrue){
						 deductionDeletes.push(data);
					 }
				 });
			}
			return deductionDeletes;
		}
		
		//操作及暂存数据收集--机构,医护人员,参保人员
		serv.intiDeducitonMedicals = function(pointCutTemps,deducitonMedicals,targetType){
			if(pointCutTemps){
				angular.forEach(pointCutTemps,function(data,index){
					if(targetType == '01'){
						if(!data.delIsTrue 
								 && data.storageIsTrue 
								 && (data.targetType == targetType || data.targetType == '04')){
							 deducitonMedicals.push(data);
						 }else if(data.isFlat
								 && (data.targetType == targetType || data.targetType == '04')){
							 deducitonMedicals.push(data);
						 }
					}else{
						if(!data.delIsTrue 
								 && data.storageIsTrue 
								 && data.targetType == targetType){
							 deducitonMedicals.push(data);
						 }else if(data.isFlat
								 && data.targetType == targetType){
							 deducitonMedicals.push(data);
						 }
					}
				 });
			}
			return deducitonMedicals;
		}
		
		//清理暂存删除时遗留数据
		var intiDel = function(pointCutTemps,warningMarks,deductionDeletes){
			angular.forEach(warningMarks,function(warningMark,indexTemp){
				angular.forEach(pointCutTemps,function(data,index){
					if(data.delIsTrue){
						deductionDeletes.push(data);
					}
				   });
				if(warningMark.deductionDeletes){
					angular.forEach(warningMark.deductionDeletes,function(datas,indexTemp){
						deductionDeletes.push(datas);
					   });
				}
			 });
			return deductionDeletes;
		}
		
		serv.intiPointCutTemps = function(pointCutTemps){
			angular.forEach(serv.intiDeductionDeletes(pointCutTemps,[]),function(datas,indexTemp){
				angular.forEach(pointCutTemps,function(data,index){
					if(data.idPointCut === datas.idPointCut){
						pointCutTemps.splice(index,1);
					}
				   });
			   });
			return pointCutTemps;
		}
		
		serv.getPointCutTemps = function(pointCutTemps,warningMarks,caseNo,caseType,
				warnMedicalData,warnStaffData,warnInsurData){
			if(!warningMarks){
				//需保存记分的数据
				   WarningAddService.warningMarks = [{
					deducitonMedicals:serv.intiDeducitonMedicals(pointCutTemps,[],'01'),//医疗机构
					deducitonStaffs:serv.intiDeducitonMedicals(pointCutTemps,[],'02'),//医护人员
					deducitonInsurs:serv.intiDeducitonMedicals(pointCutTemps,[],'03'),//参保人员
					deductionDeletes:serv.intiDeductionDeletes(pointCutTemps,[])//删除过已存库的数据
					}];
			}else{
				//需保存记分的数据
				   WarningAddService.warningMarks = [{
					deducitonMedicals:serv.intiDeducitonMedicals(pointCutTemps,[],'01'),//医疗机构
					deducitonStaffs:serv.intiDeducitonMedicals(pointCutTemps,[],'02'),//医护人员
					deducitonInsurs:serv.intiDeducitonMedicals(pointCutTemps,[],'03'),//参保人员
					deductionDeletes:serv.intiDeductionDeletes(pointCutTemps,intiDel(pointCutTemps,warningMarks,[]))//删除过已存库的数据
					}];
			}
			
			if(warnMedicalData){
 			   angular.forEach(serv.intiPointCutTemps(pointCutTemps),function(datas,indexTemp){
 				   				var isTrue = serv.initIsTrue(warnMedicalData,caseType,datas,caseNo);
		            			 if(datas.orgCode === datas.targetNo
			            				 && ('01' === datas.targetType 
			            				 || '04' === datas.targetType)
			            				 && isTrue){
		            				 var scoreData = {};
		            				 var idPointCuts = serv.getIdPointCuts(datas,1);
		            				 scoreData = datas;
		            				 scoreData.score = datas.point;
		            				 scoreData.reason = datas.reason;
		            				 warnMedicalData.scoreData.push(scoreData);
		            				 warnMedicalData.idPointCuts.push(idPointCuts);
			            		 }
         		 		});
					 warnMedicalData.score = serv.scoresNum(warnMedicalData.scoreData);
			   	}
     		 if(warnStaffData){
     			 angular.forEach(warnStaffData,function(data,index){
     				 data.score = null;
     				 data.scoreData = [];
     				 data.idPointCuts = [];
     				 angular.forEach(serv.intiPointCutTemps(pointCutTemps),function(datas,indexTemp){
   			           var isTrue = serv.initIsTrue(data,caseType,datas,caseNo);
         				 if(datas.orgCode != datas.targetNo
	            					 && isTrue
		            				 && '02' == datas.targetType){
	            				 var scoreData = {};
	            				 var idPointCuts = serv.getIdPointCuts(datas,1);
	            				 scoreData.score = datas.point;
	            				 scoreData.reason = datas.reason;
	            				 data.scoreData.push(scoreData);
	            				 data.idPointCuts.push(idPointCuts);
		            		 }
	            		 });
         			 data.score = serv.scoresNum(data.scoreData);
					   });
     		 }
     		 if(warnInsurData){
         		 angular.forEach(warnInsurData,function(warnInsurs,index){
         			 warnInsurs.score = null;
         			 warnInsurs.scoreData = [];
         			 warnInsurs.idPointCuts = [];
         			 angular.forEach(serv.intiPointCutTemps(pointCutTemps),function(datas,indexTemp){
   			           var isTrue = serv.initIsTrue(warnInsurs,caseType,datas,caseNo);
         			 	 if(datas.orgCode != datas.targetNo
	            					 && isTrue
		            				 && '03' === datas.targetType){
	            				 var scoreData = {};
	            				 var idPointCuts = serv.getIdPointCuts(datas,1);
	            				 scoreData.score = datas.point;
	            				 scoreData.reason = datas.reason;
	            				 warnInsurs.scoreData.push(scoreData);
	            				 warnInsurs.idPointCuts.push(idPointCuts);
		            		 }
         		 	});
	            		 warnInsurs.score = serv.scoresNum(warnInsurs.scoreData);
	                 });
     		 }
		}
		
		serv.findPointCutTempScose = function(warningMarks,caseNo,caseType,
				warnMedicalData,warnStaffData,warnInsurData){
			if(warnMedicalData){
				warnMedicalData.score = null; 
				warnMedicalData.scoreData=[];
				warnMedicalData.idPointCuts=[];//提供后期删除该对象引用
			}
			if(!caseNo){
				var pointCutTemps = serv.getWarningMarks([],warningMarks,caseNo,caseType);
				serv.getPointCutTemps(pointCutTemps,warningMarks,caseNo,caseType,
						warnMedicalData,warnStaffData,warnInsurData);
			}else{
				serv.getPointCutTempScose({caseNo:caseNo,caseType:caseType}).
			       success(function(res){
		    		   if(res.pointCutTemps){
		    			 //获取数据
  						 var pointCutTemps = serv.getWarningMarks(deleteDedus(res.pointCutTemps),warningMarks,caseNo,caseType);
  						 //统计记分值
  						 serv.getPointCutTemps(pointCutTemps,warningMarks,caseNo,caseType,
  									warnMedicalData,warnStaffData,warnInsurData);
	    			   }
			       });
			}
			return true;
		}
		//处理记分信息
		serv.getWarningMarks = function(pointCutTemps,warningMarks,caseNo,caseType){
			if(warningMarks){
				angular.forEach(warningMarks,function(warningMark,indexTemp){
					if(warningMark.deductionDeletes){
						angular.forEach(warningMark.deductionDeletes,function(datas,indexTemp){
							angular.forEach(pointCutTemps,function(data,index){
								if(data.idPointCut === datas.idPointCut){
									pointCutTemps.splice(index,1);
								}
							   });
						   });
					}
					if(warningMark.deducitonMedicals){
						angular.forEach(warningMark.deducitonMedicals,function(datas,index){
							if(!datas.storageIsTrue){
								pointCutTemps.push(datas);
							}
						   });
					}
					if(warningMark.deducitonStaffs){
						angular.forEach(warningMark.deducitonStaffs,function(datas,index){
							if(!datas.storageIsTrue){
								pointCutTemps.push(datas);
							}
						   });
					}
					if(warningMark.deducitonInsurs){
						angular.forEach(warningMark.deducitonInsurs,function(datas,index){
							if(!datas.storageIsTrue){
								pointCutTemps.push(datas);
							}
						   });
					}
				 });
			}
			return pointCutTemps;
		}
		serv.scoresNum = function(data){//计算分值
			 var pointNum = '0.0';
			 var count = 0;
	 		angular.forEach(data,function(datas,index){
	 			if(datas.score){
					pointNum = (parseFloat(pointNum) + parseFloat(datas.score)).toFixed(1);
					count++; 
	 			}
			   });
	 		if(count == 0){
	 			return null;
	 		}
			return pointNum;
		}
		//组织删除数据
		serv.delIdPointCuts = function(data,deductionDeletes,num){//num:0:记录审批前临时记分信息,1:记录暂存记分信息
			if(num == 0){
				if(data.idPointCuts){
					angular.forEach(data.idPointCuts,function(idPointCutData,indexTemp){
						if(idPointCutData.idPointCut){
							deductionDeletes.push(idPointCutData);
						}
					});
				}
			}else if(num == 1){
				if(!data.idPointCut){
					deductionDeletes.push(data);
				}
			}
			return deductionDeletes;
		}
		//删除的信息转存
		var delParams = function(datas){
			return {ruleId:datas.ruleId,isFlat:datas.isFlat,delIsTrue:datas.delIsTrue,storageIsTrue:datas.storageIsTrue,caseType:datas.caseType,targetNo:datas.targetNo};
		}
		//条件
		var isParamsTrue = function(dataNum,datas){
			return dataNum.ruleId == datas.ruleId && dataNum.isFlat == datas.isFlat &&　dataNum.delIsTrue　== datas.delIsTrue
			&& dataNum.storageIsTrue == datas.storageIsTrue && dataNum.caseType == datas.caseType && dataNum.targetNo == datas.targetNo;
		}
		//删除该对象扣分信息
		serv.deleteMark = function(warningMarks,mark,data,warnStaffData,warnInsurData){//后两参数（warnStaffData,warnInsurData）只在登记时删除才传
			if(warningMarks){
				var staffsNums = [];//医护人员
				var insursNums = [];//参保人
				angular.forEach(warningMarks,function(warningMark,indexTemp){
					if(indexTemp == 0){
						warningMark.deductionDeletes = serv.delIdPointCuts(data,warningMark.deductionDeletes,0);//num:0:记录审批前临时记分信息
						if(warnStaffData){
							angular.forEach(warnStaffData,function(datas,index){
								datas.score = null;
								if(datas.scoreData){
									angular.forEach(datas.scoreData,function(scoreData,indexScoreData){
										datas.scoreData.splice(indexScoreData,1);
									});
								}
								warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,0);//num:0:记录审批前临时记分信息
							});
						}
						if(warnInsurData){
							angular.forEach(warnInsurData,function(datas,indexStaffs){
								datas.score = null;
								if(datas.scoreData){
									angular.forEach(datas.scoreData,function(scoreData,indexScoreData){
										datas.scoreData.splice(indexScoreData,1);
									});
								}
								warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,0);//num:0:记录审批前临时记分信息
							});
						}
						if(mark == 1){
							angular.forEach(warningMark.deducitonStaffs,function(datas,indexStaffs){
								if(datas.caseType == '01'){
									if(data.code == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										staffsNums.push(delParams(datas));
									}
								}else if(datas.caseType == '02'){
									if(data.selfCode == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										staffsNums.push(delParams(datas));
									}
								}
							   });
							angular.forEach(warningMark.deducitonInsurs,function(datas,indexInsurs){
								if(datas.caseType == '01'){
									if(data.code == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										insursNums.push(delParams(datas));
									}
								}else if(datas.caseType == '02'){
									if(data.selfCode == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										insursNums.push(delParams(datas));
									}
								}
							   });
							warningMarks = [{
								deducitonMedicals:[],//医疗机构
								deducitonStaffs:warningMarks[0].deducitonStaffs,//医护人员
								deducitonInsurs:warningMarks[0].deducitonInsurs,//参保人员
								deductionDeletes:warningMarks[0].deductionDeletes//删除过已存库的数据
								}];
						}
						if(warningMark.deducitonStaffs && mark == 2){
							angular.forEach(warningMark.deducitonStaffs,function(datas,index){
								if(datas.caseType == '01'){
									if(data.code == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										staffsNums.push(delParams(datas));
									}
								}else if(datas.caseType == '02'){
									if(data.selfCode == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										staffsNums.push(delParams(datas));
									}
								}
							   });
						}
						if(warningMark.deducitonInsurs && mark == 3){
							angular.forEach(warningMark.deducitonInsurs,function(datas,index){
								if(datas.caseType == '01'){
									if(data.code == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										insursNums.push(delParams(datas));
									}
								}else if(datas.caseType == '02'){
									if(data.selfCode == datas.targetNo){
										warningMark.deductionDeletes = serv.delIdPointCuts(datas,warningMark.deductionDeletes,1);//num:1:记录暂存记分信息
										insursNums.push(delParams(datas));
									}
								}
							   });
						}
					}
				   });
				if(staffsNums){
					angular.forEach(staffsNums,function(dataNum,indexNum){
						angular.forEach(warningMarks[0].deducitonStaffs,function(datas,index){
							if(isParamsTrue(dataNum,datas)){
								warningMarks[0].deducitonStaffs.splice(index,1);
							}
						});
					});
				}
				if(insursNums){
					angular.forEach(insursNums,function(dataNum,indexNum){
						angular.forEach(staffsNums,function(datas,index){
							angular.forEach(warningMarks[0].deducitonInsurs,function(datas,index){
								if(isParamsTrue(dataNum,datas)){
									warningMarks[0].deducitonInsurs.splice(index,1);
								}
							});
						});
					});
				}
			}else{
				warningMarks = [{
					deducitonMedicals:[],//医疗机构
					deducitonStaffs:[],//医护人员
					deducitonInsurs:[],//参保人员
					deductionDeletes:[]//删除过已存库的数据
					}];
				serv.deleteMark(warningMarks,mark,data);//初始化:warningMarks
			}
			WarningAddService.warningMarks = warningMarks;//serv.delDatas(warningMarks,data,mark);
		}
		//清理暂存删除时遗留数据
		var deleteDedus = function(pointCutTemps){
			if(pointCutTemps && !WarningAddService.warningMarks){
				angular.forEach(pointCutTemps,function(datas,indexTemp){
					if(datas.delIsTrue && datas.storageIsTrue && !datas.idPointCut){
						pointCutTemps.splice(indexTemp,1);
					}
				   });
			}
			return pointCutTemps;
		}
		//清理暂存删除时遗留数据
		var delDedus = function(pointCutTemps,warningMarkServices){
			if(pointCutTemps && warningMarkServices){
				angular.forEach(pointCutTemps,function(datas,indexTemp){
					if(datas.delIsTrue){
						warningMarkServices.push(datas);
					}
				   });
			}
			return warningMarkServices;
		}
	}]);
	
})();